package com.nuoniu.sibanyun.controller.cm;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.cm.CmContract;
import com.nuoniu.sibanyun.service.cm.ICmContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 合同管理主表 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2021-10-21
 */
@RestController
@RequestMapping("/cm/contract")
@Api(tags = "合同管理")
public class CmContractController extends BaseController {

    @Autowired
    private ICmContractService contractService;

    @ApiOperation("获取编码")
    @GetMapping("getCode")
    public HttpResult getCode(Integer typeId) {
        return HttpResult.ok(contractService.getCode(typeId));
    }

    @ApiOperation("保存合同")
    @PostMapping("save")
    public HttpResult save(@RequestBody CmContract contract) {
//        if(StringUtils.isBlank(contract.getContractCode())) {
//            throw new NuoNiuException("编码不能为空");k
//        }

        if(StringUtils.isBlank(contract.getContractName())) {
            throw new NuoNiuException("合同名称不能为空");
        }

        if(contract.getContractTypeId() == null) {
            throw new NuoNiuException("请选择合同类型");
        }

        if(contract.getOrderDetailList() != null && contract.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("合同明细不能为空");
        }

        contractService.saveContract(contract);
        return HttpResult.ok();
    }

    @ApiOperation("获取合同详情")
    @GetMapping("getDetail")
    public HttpResult getDetail(Long id) {
        return HttpResult.ok(contractService.getDetail(id));
    }

    @ApiOperation("删除合同")
    @DeleteMapping("delete")
    public HttpResult delete(Long id) {
        contractService.delete(id);
        return HttpResult.ok();
    }

    @GetMapping("examine")
    @ApiOperation("单据审核/弃审")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
        contractService.examine(id, status);
        return HttpResult.ok();
    }

    @ApiOperation("归档")
    @GetMapping("file")
    public HttpResult file(Long id) {
        contractService.file(id);
        return HttpResult.ok();
    }

    @ApiOperation("中止")
    @GetMapping("close")
    public HttpResult close(Long id) {
        contractService.close(id);
        return HttpResult.ok();
    }

    @ApiOperation("变更合同")
    @PostMapping("changeContract")
    public HttpResult changeContract(@RequestBody CmContract cmContract) {
        if(cmContract.getId() == null) {
            throw new NuoNiuException("找不到对应合同");
        }

        if(StringUtils.isBlank(cmContract.getChangeCause())) {
            throw new NuoNiuException("变更原因不能为空");
        }

        if(cmContract.getOrderDetailList() == null || cmContract.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("合同明细不能为空");
        }

        contractService.changeContract(cmContract);
        return HttpResult.ok();
    }

    @ApiOperation("获取变更列表")
    @GetMapping("getChangeList")
    public HttpResult getChangeList() {
        return HttpResult.ok(contractService.getChangeList());
    }

    @ApiOperation("获取变更详情")
    @GetMapping("getChangeDetail")
    public HttpResult getChangeDetail(Integer changeId) {
        return HttpResult.ok(contractService.getChangeDetail(changeId));
    }
}
